require"common"

--=========================单式复式计算=================================================
function MultSimpComputeSf14(jItemstr,djStr,butPower,m,n,butType)
	local queryStr = {}    ---查询指令串
	local bullStr = {}		----开奖指令串
	local bingoNum = 0
	local result = {0,0}
	local bingopos = {0,0,0,0,0,0,0,0,0,0,0,0,0,0}
	local len = 0
	if butType == '1' then
		jitemLen = string.len(jItemstr)
		queryStr[1]={}
		for j=1,jitemLen do
			queryStr[1][j] = string.sub(jItemstr,j,j+n-1)
		end
	else
		for i=1,#jItemstr do
			jitemLen = string.len(jItemstr[i])
			queryStr[i]={}
			for j=1,jitemLen do
				queryStr[i][j] = string.sub(jItemstr[i],j,j+n-1)
			end
		end

	end
	for i=1,m do
		bullStr[i] = string.sub(djStr,i,i+n-1)
	end
	if butType == '1' then
		--for i = 1,table.getn(jItemstr) do
			for j,k in ipairs(bullStr) do
				if queryStr[1][j] == k or k == '*' then
					bingoNum = bingoNum + 1
				end
			end
			if bingoNum == m then
				bingoNum = 0
				result[1] = result[1] + 1

			elseif bingoNum == m - 1 then
				bingoNum = 0
				result[2] = result[2] + 1
			end
		--end
		result[1] = result[1]*butPower
		result[2] = result[2]*butPower
	elseif butType == '2' then
	--如果比赛有延期，这这场比赛都默认猜对。中奖注数=1*这场选的个数
		nNum = 1
		CancelPos = {0,0,0,0,0,0,0,0,0,0,0,0,0,0}
		for i=1,m do
			for _,k in ipairs(queryStr[i]) do
				if k == bullStr[i] or bullStr[i] == '*'  then
					bingoNum = bingoNum + 1
					bingopos[i] = 1
					if bullStr[i] == '*' then
						nNum = nNum * (#queryStr[i])
						CancelPos[i] = 1
					end

					break
				end
			end

		end
		if bingoNum == m then
			result[1] = nNum*butPower
			for v = 1,m do
				if CancelPos[v] ~= 1 then
					result[2] = result[2] + (string.len(jItemstr[v]) - 1)
				end

			end
			result[2] = result[2] * nNum * butPower
		elseif bingoNum == 13 then
			for i = 1, 14 do
				if bingopos[i] == 0 then
					len = table.getn(queryStr[i])*nNum
				end
			end
			result[2] =len*butPower
		else
			return result
		end
	end


	return result

end
--================================================================================

--判断字符串
function judge_str( num , max)
        local out = ""
        local st = {}
        if num == nil then return nil end
        local len = string.len( num )

        for i=1,len do
                c = string.sub(num,i,i)
                if not (tonumber(c) == 0  or tonumber(c) == 1 or tonumber(c) == 3 ) then return nil end
        end

        return true
end

--返回注数,倍投数 , 投注串, 指令串
function random_sf14( butType, inStr )
	local butNum = 1
	local butPower = 1
	local zlStr = ""
	local butStr = ""
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#B" )
	itemLen = table.getn( itemList )
	qjStr = string.gsub( inStr, "#B", "0")
	if itemLen > 2 or itemLen < 1 or string.find(qjStr,"%D") then return 0 end

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower >99 or butPower < 1 then return 0 end
	--单式机选  1402#注码#B倍数
	if butType == '1' then
		butNum = tonumber(itemList[1])
		for i=1,butNum do
			local nums = selectNumber(14,3,1,0)
			zlStr = zlStr..nums[1]..nums[2]..nums[3]..nums[4]..nums[5]..nums[6]..nums[7]..nums[8]..nums[9]..nums[10]..nums[11]..nums[12]..nums[13]..nums[14]
			if i<butNum then zlStr = zlStr.."#" end
		end
		zlStr = zlStr.."#B"..butPower
		butNum,butPower,butStr = check_sf14('1', zlStr)
	end

	return butNum, butPower, butStr, zlStr


end

--返回注数,倍投数,投注串
function check_sf14( butType, inStr)
	local qxStr;
	local qjStr;
	local butNum = 0;
	local butPower = 1;
	local butStr = "";
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#B" )
	itemLen = table.getn( itemList )

	qjStr = string.gsub( inStr, "#B", "0")
	qxStr = string.gsub( qjStr, "#", "0")
	if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then return 0 end

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower >99 or butPower < 1 then return 0 end

	--单式自选 1401#注码#B倍数  1401#31030100111000#01030100111000
	if  butType == '1' then
		xItem = Split(itemList[1], "#")
		butNum = #xItem
		if butNum > 5 or butNum < 1  then
			return 0
		end
		for i=1,#xItem do
			if string.len(xItem[i]) ~= 14 then return 0 end
			if not judge_str(xItem[i]) then return 0 end
		end
	--复式自选 1403#A#B#C#D#E#F#G#H#I#J#K#L#M#N
	elseif butType == '2' then
		xItem = Split(itemList[1], "#")
		if #xItem ~= 14 then return 0 end
		butNum = 1
		for i=1,#xItem do
			if string.len(xItem[i]) > 3 then return 0 end
			if not judge_str(xItem[i])  then return 0 end
			butNum = butNum*string.len(xItem[i])
		end
	else
		return 0
	end;
	return butNum,butPower,inStr,inStr
end
--=======================中奖查询=====================================
function query_sf14(butType,queryStr,djStr)
	local butPower = 1
	local itemList = {}
	local result = {0,0}
	itemList = Split( queryStr, "#B" )

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end

	if  butType == '1' then
		jItem = Split(itemList[1], "#")
		for i=1, #jItem do
			tmpresult =MultSimpComputeSf14(jItem[i],djStr,butPower,14,1,butType)
			for j=1,#result do
				result[j] = result[j] + tmpresult[j]
			end
		end

	elseif butType == '2' then
		jItem = Split(itemList[1], "#")
		result = MultSimpComputeSf14(jItem,djStr,butPower,14,1,butType)

	else
		return 0
	end;

	return result[1]..","..result[2]
end


--=========================================================================
--print(random_sf14('1',"5#B11"))
--print(check_sf14('1',"33333111110000#B1"))
--print(check_sf14('2',"310#310#031#0#0#0#0#0#0#0#1#0#1#31"))
--print(query_sf14('2',"310#310#310#310#310#3#310#3#3#3#3#0#0#310#B1","0313130333300*"))
--print(query_sf14('1',"30303313133101#B1","11310303133001"))
--print(string.len("11310303133001"))
